package com.ssi.tatts.model.hibernate.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.etu.jsonservices.model.IGenericDAO;
import com.etu.jsonservices.model.PostProcessorDAODecorator;
import com.ssi.tatts.model.hibernate.domainobjects.Metric;
import com.ssi.tatts.model.hibernate.domainobjects.ProductivityTypeHeavy;

/**
 * @author uye
 */
public class ProductivityTypeHeavyPostProcessorDAO extends
        PostProcessorDAODecorator<ProductivityTypeHeavy, Long> {

    @Autowired
    private SessionFactory      sessionFactory;

    private static final String createQuery = " from Metric"
                                                    + " where productivityType ="
                                                    + " :productivityType" + "";

    /**
     * Constructs and wraps the decorated DAO.
     * 
     * @param decoratedDao
     *            The decorated DAO
     */
    public ProductivityTypeHeavyPostProcessorDAO(
            final IGenericDAO<ProductivityTypeHeavy, Long> decoratedDao) {
        super(decoratedDao);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final ProductivityTypeHeavy executePostProcessor(
            final ProductivityTypeHeavy obj) {

        super.executePostProcessor(obj);
        Session session = sessionFactory.getCurrentSession();

        @SuppressWarnings("unchecked")
        List<Metric> metrics = session.createQuery(createQuery)
                .setParameter("productivityType", obj.getId()).list();

        obj.setMetrics(metrics);

        return obj;
    }

}
